package com.spinnerselect;

import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import com.utils.GsonUtil;
import com.utils.ItemBeen;
import com.uzmap.pkg.uzcore.UZWebView;
import com.uzmap.pkg.uzcore.uzmodule.UZModule;
import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;
import com.weight.NiceSpinner;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
/**
 * 作者: 赵航
 * 创建时间: 2018/5/30 18:14
 * 描述:
 */
public class NaXiangSpinner extends UZModule {

    private ItemBeen    mBeens;
    private View        mMain;
    private NiceSpinner mNiceSpinner;

    public NaXiangSpinner(UZWebView webView) {
        super(webView);
    }

    //打开条目选择器
    public void jsmethod_openItemSelect(UZModuleContext moduleContext) {
        String jsonList = moduleContext.optString("paramString", "初始化数据失败，请再次点击");
        mBeens = GsonUtil.parseJsonToBean(jsonList, ItemBeen.class);
        mNiceSpinner = new NiceSpinner(mContext);
        List<String> dataList = new ArrayList<>();
        for (int i = 0; i < mBeens.getList().size(); i++) {
            dataList.add(mBeens.getList().get(i).getName());
        }
        mNiceSpinner.attachDataSource(dataList);
        FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams
                (FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        params2.leftMargin = 200;
        params2.rightMargin = 200;
        params2.topMargin=500;
        mContext.addContentView(mNiceSpinner, params2);
    }

    //已经选择的条目
    public void jsmethod_openChioceItem(final UZModuleContext moduleContext) {
        mNiceSpinner.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for (int i = 0; i < mBeens.getList().size(); i++) {
                        if (position==i){
                            JSONObject object = new JSONObject();
                            try {
                                object.put("subject", mBeens.getList().get(i).getName());
                                moduleContext.success(object, false);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
            }
        });
    }


//        JSONObject object = new JSONObject();
//        try {
//            if (mBeens != null) {
//                object.put("city", mBeens.getList().get(0).getName() + mBeens.getList().get(1).getName() + mBeens.getList().get(2).getName() + mBeens.getList().get(3).getName());
//                //                    object.put("city", mBeens.getList().get(2).getName());
//                //                    object.put("city", mBeens.getList().get(3).getName());
//                //                    object.put("city", mBeens.getList().get(4).getName());
//                //                    object.put("city", mBeens.getList().get(5).getName());
//                //                    object.put("city", mBeens.getList().get(6).getName());
//
//
//                moduleContext.success(object, true);
//            }
//            //            object.put("city", "调用了");
//            //            moduleContext.success(object, true);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
}
